﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace xCarLinkMP3Organizer
{
    public static class PlayList
    {
        public static List<string> Load(string path)
        {
            if (File.Exists(path))
            {
                try
                {
                    var fi = new FileInfo(path);
                    var p = path;

                    bool deleteTemp = false;
                    try
                    {
                        if (fi.Extension == ".m3u")
                        {
                            p = ConvertToUnicode(path);
                            deleteTemp = true;
                        }

                        return GetFiles(p);
                    }
                    finally
                    {
                        if (deleteTemp)
                            File.Delete(p);
                    }
                }
                catch (Exception e)
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        private static List<string> GetFiles(string path)
        {
            List<string> l = null;
            using (var reader = new StreamReader(path))
            {
                string text;

                try
                {
                    text = reader.ReadLine();
                    if ((!String.IsNullOrEmpty(text)) && (text == "#EXTM3U"))
                    {
                        l = new List<string>();
                        text = reader.ReadLine();
                        while ((!reader.EndOfStream) || (!String.IsNullOrEmpty(text)))
                        {
                            string title = null;
                            int i = text.IndexOf(",") + 1;

                            if ((text.IndexOf("#EXTINF:") != -1) && (i != 0)) //Exists #EXTINF
                            {
                                title = text.Substring(i);

                                //Get the path
                                text = reader.ReadLine();
                            }
                            else
                            {
                                var fi = new FileInfo(text);
                                title = fi.Name;
                            }

                            //Add to dictionary
                            l.Add(text);
                            text = reader.ReadLine();
                        }
                    }
                }
                catch (Exception e)
                {
                    l = null;
                }
                return l;
            }
        }

        private static string ConvertToUnicode(string path)
        {
            var temp = Path.GetTempFileName();

            var sr = new StreamReader(path, Encoding.Default);
            var sw = new StreamWriter(temp, false, Encoding.Unicode);

            sw.Write(sr.ReadToEnd());
            sw.Close();
            sr.Close();

            return temp;
        }
    }
}
